import getNetWork from '../../libs/getNetWork.js';
import portlet from '../../components/portlet.vue';
let id = 1000;
export default {
    components:{
        portlet,
    },
    data() {
        return {
            data2: [{
              id: 1,
              label: '一级 1',
              children: [{
                id: 4,
                label: '二级 1-1',
                children: [{
                  id: 9,
                  label: '三级 1-1-1'
                }, {
                  id: 10,
                  label: '三级 1-1-2'
                }]
              }]
            }, {
              id: 2,
              label: '一级 2',
              children: [{
                id: 5,
                label: '二级 2-1'
              }, {
                id: 6,
                label: '二级 2-2'
              }]
            }, {
              id: 3,
              label: '一级 3',
              children: [{
                id: 7,
                label: '二级 3-1'
              }, {
                id: 8,
                label: '二级 3-2'
              }]
            }],
            defaultProps: {
              children: 'children',
              label: 'menu_name'
            },
            name:'',
            checked:null,
            loading:false,
            showCheck:true,
            dialogFormVisible0:false,
            dialogFormVisible1:false,
            dialogFormVisible2:false,
            form0: {
              name: '',
            },
            form1: {
              name: '',
            },
            form2: {
              name: '',
            },
            operateData:[],
        };
    },

    mounted:function() {
        this.$nextTick(()=>{
            this.getData();
        })
        
    },
    methods:{
        getData(){
                this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/menu/getMenuTree',
                    successCallback(res){
                        that.data2=res.body.dataArr;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.get(this,data);

        },
        changeData(){
            this.loading=true;
                let that=this;
                let data={
                    url:that.getNetDataAddress+'/role/updateRole',
                    data:{
                        page:that.page,
                        rows:that.rows,
                        rolename:that.name,
                    },
                    successCallback(res){
                        that.tableData=res.body.dataObj.rows;
                        that.total=res.body.dataObj.records;
                        that.loading=false;
                    },
                    defaultCallback(){
                        that.loading=false;
                    }
                }
                getNetWork.post(this,data);
                this.getData();
        },
        search(){
            this.getData();
        },
        add(){
            this.dialogFormVisible0=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/addMenu',
                data:{
                    menu_name:that.form0.name,
                    parent_id:that.checked.id,
                },
                successCallback(res){
                    that.$message('添加成功！');
                    that.getData();
                }
            }
            getNetWork.post(this,data); 
        },
        update(){
            this.dialogFormVisible1=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/updateMenu',
                data:{
                    menu_name:that.form1.name,
                    id:that.checked.id,
                },
                successCallback(res){
                    that.$message('修改成功！');
                    that.getData();
                }
            }
            getNetWork.post(this,data); 
        },


        append(store, data) {
            this.dialogFormVisible0=true;
            this.checked=data;
        },
        beforeUpdate(store, data) {
            this.dialogFormVisible1=true;
            this.checked=data;
            this.form1.name=data.menu_name;
        },
        beforePower(store, data1) {
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/getMenuOperate',
                data:{
                    menu_id:data1.id,
                },
                successCallback(res){
                    that.operateData=res.body.dataArr;
                }
            }
            getNetWork.post(this,data);
            this.dialogFormVisible2=true;
        },
        remove(store, data1) {
            this.$confirm('此操作将删除该项, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {

                let that=this;
                let data={
                    url:that.getNetDataAddress+'/menu/delMenu',
                    data:{
                        id:data1.id,
                    },
                    successCallback(res){
                        that.$message('删除成功！');
                        that.getData();
                    }
                }
                console.log('del');
                getNetWork.post(this,data);
            }).catch(() => {
                console.log('error');
            });
        },
        power(){
            this.dialogFormVisible2=false;
            let that=this;
            let data={
                url:that.getNetDataAddress+'/menu/updateMenu',
                data:{
                    menu_name:that.form2.name,
                    id:that.checked.id,
                },
                successCallback(res){
                    that.$message('修改成功！');
                    that.getData();
                }
            }
            getNetWork.post(this,data); 
        },
        renderContent(h, { node, data, store }) {
            if(data.level==0){
                return (
                  <span>
                    <span>
                      <span>{node.label}</span>
                    </span>
                    <span style="position:absolute;right:20px;top:0px">
                      <el-button size="mini" on-click={ () => this.append(store, data) }>新增
                      </el-button>
                    </span>
                  </span>);
            }else if(data.level==1){
                return (
                  <span>
                    <span>
                      <span>{node.label}</span>
                    </span>
                    <span style="position:absolute;right:20px;top:0px">
                      <el-button size="mini" on-click={ () => this.append(store, data) }>新增</el-button>
                      <el-button size="mini" on-click={ () => this.remove(store, data) }>删除</el-button>
                      <el-button size="mini" on-click={ () => this.beforeUpdate(store, data) }>修改</el-button>
                    </span>
                  </span>);
            }else if(data.level==2){
                return (
                  <span>
                    <span>
                      <span>{node.label}</span>
                    </span>
                    <span style="position:absolute;right:20px;top:0px">
                      <el-button size="mini" on-click={ () => this.remove(store, data) }>删除</el-button>
                      <el-button size="mini" on-click={ () => this.beforeUpdate(store, data) }>修改</el-button>
                      <el-button size="mini" on-click={ () => this.beforePower(store, data) }>授权</el-button>
                    </span>
                  </span>);
            }
            
        }
    }
};
